package com.ccs.lotterySystem.common.pojo;

import com.ccs.lotterySystem.common.errorcode.ErrorCode;
import com.ccs.lotterySystem.common.errorcode.GlobalErrorCodeConstants;
import org.springframework.util.Assert;

public class CommonResult<T> {
    private Integer code;
    private T data;
    private String msg;

    public static <T> CommonResult<T> success(T data){
        CommonResult<T> result=new CommonResult<>();

        result.code= GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data=data;
        result.msg=GlobalErrorCodeConstants.SUCCESS.getMessage();
        return result;
    }

    public static <T> CommonResult<T> error(Integer code,String msg){
        Assert.isTrue(!GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.equals(code),"code不是错误的异常");
        CommonResult<T> result=new CommonResult<>();
        result.code=GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode();
        result.msg=GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getMessage();
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMessage());
    }
}
